package com.chen.manager.controller;

import com.chen.manager.service.ProductSpecService;
import com.chen.model.entity.product.Product;
import com.chen.model.entity.product.ProductSpec;
import com.chen.model.vo.common.Result;
import com.chen.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author cgj
 * @date 3/9/25 2:59 PM
 */
@RestController
@RequestMapping("/admin/product/productSpec")

public class ProductSpecController {
    @Autowired
    private ProductSpecService productSpecService;
    /**
     * 加载所有商品规格
     */
    @GetMapping("")
    public Result<List<ProductSpec>> selectAll(){
        return Result.build(productSpecService.selectAll(),ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        productSpecService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     *更新规格
     * @param productSpec
     * @return
     */
    @PutMapping("/updateProductSpec")
    public Result updateProductSpec(@RequestBody ProductSpec productSpec){
        productSpecService.updateProductSpec(productSpec);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存规格
     * @param productSpec
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody ProductSpec productSpec){
        productSpecService.save(productSpec);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<PageInfo<ProductSpec>>  selectByPage(@PathVariable Integer pageNum,
                                                       @PathVariable Integer pageSize){
              PageInfo<ProductSpec> pageInfo=productSpecService.selectByPage(pageNum,pageSize);

        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
